#include <math.h>

#import <Foundation/Foundation.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/UIKit.h>
#import <UIKit/UITouch.h>

#import "MediaPanel.h"
#import "QtKey.h"

typedef enum
{
	UIButtonTypeCustom = 0,
	UIButtonTypeRoundedRect,
	UIButtonTypeDetailDisclosure,
	UIButtonTypeInfoLight,
	UIButtonTypeInfoDark,
	UIButtonTypeContactAdd,
} UIButtonType;

enum
{
	UIControlEventTouchDown           = 1 <<  0,
	UIControlEventTouchDownRepeat     = 1 <<  1,
	UIControlEventTouchDragInside     = 1 <<  2,
	UIControlEventTouchDragOutside    = 1 <<  3,
	UIControlEventTouchDragEnter      = 1 <<  4,
	UIControlEventTouchDragExit       = 1 <<  5,
	UIControlEventTouchUpInside       = 1 <<  6,
	UIControlEventTouchUpOutside      = 1 <<  7,
	UIControlEventTouchCancel         = 1 <<  8,
	
	UIControlEventValueChanged        = 1 << 12,
	
	UIControlEventEditingDidBegin     = 1 << 16,
	UIControlEventEditingChanged      = 1 << 17,
	UIControlEventEditingDidEnd       = 1 << 18,
	UIControlEventEditingDidEndOnExit = 1 << 19,
	
	UIControlEventAllTouchEvents      = 0x00000FFF,
	UIControlEventAllEditingEvents    = 0x000F0000,
	UIControlEventApplicationReserved = 0x0F000000,
	UIControlEventSystemReserved      = 0xF0000000,
	UIControlEventAllEvents           = 0xFFFFFFFF
};

@class UIButton;
@class UIEvent;

@implementation MediaPanel

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame: frame];
	
	keySets = [[NSMutableDictionary alloc] initWithCapacity:8];
	
	// [self setMultipleTouchEnabled:YES];
	[self setImage:[UIImage imageNamed:@"MediaPanel.png"]];
	[self setUserInteractionEnabled:YES];	// UIImageViewはNOになっているのでYESにしないとtouchesイベントが拾えない、subviewのUIButtonが反応しない
	
	[self addSubview:[self buttonWithFrame:CGRectMake(  1,   6, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_MediaPrevious]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(120,   2, 76, 76) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_MediaPlay]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(255,   6, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_MediaNext]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(200,  66, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_VolumeMute]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake( 60,  66, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_MediaStop]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(256, 132, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_VolumeUp]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(256, 192, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_VolumeDown]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake( 46, 182, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Back]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(130, 198, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Up]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(130, 350, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Down]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake( 54, 274, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Left]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(204, 274, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Right]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(130, 274, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Return]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(130, 118, 64, 64) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Meta], [NSNumber numberWithInt:Key_Alt], [NSNumber numberWithInt:Key_Return], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(252, 376, 64, 32) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:'E'], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(  2, 376, 64, 32) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:'I'], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(  0, 118, 52, 52) keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Menu]]]]];
	
	return self;
}

- (void)dealloc
{
	[super dealloc];
}

- (BOOL)canBecomeFirstResponder
{
	return YES;
}

- (UIButton *)buttonWithFrame:(CGRect)frame keyArray:(NSArray *)keys
{
	UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
	// [b setRotationBy:90.0];
	[b setFrame:frame];
	[b setShowsTouchWhenHighlighted:YES];
	// [b setBackgroundColor:[UIColor darkGrayColor]];
	// [b setFont:[UIFont systemFontOfSize:36.0]];
	// [b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	// [b setBackgroundImage:[UIImage imageNamed:@"Key.png"] forState:UIControlStateNormal];
	// [b setTitle:@"Something" forState:UIControlStateNormal];
	[b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpOutside];
	
	[keySets setObject:keys forKey:[NSNumber numberWithInt:b]];
	
	return b;
}

- (void)buttonPressed:(UIButton *)button
{
	if ([[NSUserDefaults standardUserDefaults] boolForKey: @"enable_click_sound"])
		AudioServicesPlaySystemSound(1105);
	
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = 0; set != [sets count]; ++set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyPressed:keyCode];
		}
	}
}

- (void)buttonReleased:(UIButton *)button
{
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = ([sets count] - 1); 0 <= set; --set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyReleased:keyCode];
		}
	}
}

@end
